package com.yc.sole.erp.controller;

import com.yc.sole.erp.biz.MoldBiz;
import com.yc.sole.erp.request.MoldQueryReq;
import com.yc.sole.erp.request.MoldReq;
import com.yc.sole.erp.response.MoldResp;
import com.yc.sole.framework.mybatis.page.BaseBatchIdsReq;
import com.yc.sole.framework.mybatis.page.BasePageReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.validate.DeleteGroup;
import com.yc.sole.framework.validate.SaveGroup;
import com.yc.sole.framework.validate.UpdateGroup;
import com.yc.sole.framework.web.PreAuth;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 模具管理
 *
 * @author yizuomin
 * @date 2023-05-07
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
public class MoldController {

    private final MoldBiz moldBiz;

    /**
     * 新增模具表
     *
     * @param moldReq
     * @return RestResult
     */
    @PreAuth(value = "erp:mold:save", description = "新增模具")
    @PostMapping("/api/erp/mold/saveMold")
    public Result<Void> saveMold(@RequestBody @Validated(SaveGroup.class) MoldReq moldReq) {
        moldBiz.saveMold(moldReq);
        return Result.success();
    }

    /**
     * 通过Id修改模具
     *
     * @param moldReq
     * @return RestResult
     */
    @PreAuth(value = "erp:mold:update", description = "修改模具")
    @PostMapping("/api/erp/mold/updateMoldById")
    public Result<Void> updateMoldById(@RequestBody @Validated(UpdateGroup.class) MoldReq moldReq) {
        moldBiz.updateMoldById(moldReq);
        return Result.success();
    }

    /**
     * 通过Id查询模具
     *
     * @param moldId
     * @return RestResult
     */
    @PreAuth(value = "erp:mold:select", description = "通过Id查询模具")
    @PostMapping("/api/erp/mold/selectMoldById/{moldId}")
    public Result<MoldResp> selectMoldById(@PathVariable Long moldId) {
        return Result.data(moldBiz.selectMoldById(moldId));
    }

    /**
     * 通过名称查询模具
     *
     * @param moldQueryReq
     * @return RestResult
     */
    @PreAuth(value = "erp:mold:select", description = "通过名称查询模具")
    @PostMapping("/api/erp/mold/selectMoldByName")
    public Result<List<MoldResp>> selectMoldByName(@RequestBody MoldQueryReq moldQueryReq) {
        return Result.data(moldBiz.selectMoldByName(moldQueryReq.getMoldName()));
    }

    /**
     * 通过id批量删除模具表
     *
     * @param baseDeleteReq
     * @return RestResult
     */
    @PreAuth(value = "erp:mold:delete", description = "删除模具")
    @PostMapping("/api/erp/mold/deleteMold")
    public Result<Void> deleteMold(@RequestBody @Validated(DeleteGroup.class) BaseBatchIdsReq baseDeleteReq) {
        moldBiz.deleteMoldByIds(baseDeleteReq.getIds());
        return Result.success();
    }

    /**
     * 导出
     *
     * @param moldQueryReq
     * @return RestResult
     */
    @PreAuth(value = "erp:mold:select", description = "导出模具")
    @PostMapping("/api/erp/mold/export")
    public void export(@RequestBody MoldQueryReq moldQueryReq, HttpServletResponse response) {
        moldBiz.export(moldQueryReq, response);
    }

    /**
     * 导入
     *
     * @return RestResult
     */
    @PreAuth(value = "erp:mold:select", description = "导入模具")
    @PostMapping("/api/erp/mold/import")
    public Result<Void> importMold(MultipartFile file) {
        moldBiz.importMold(file);
        return Result.success();
    }


    /**
     * 下载模具模板
     *
     * @return RestResult
     */
    @PreAuth(value = "erp:mold:export", description = "下载模具模板")
    @PostMapping("/api/erp/mold/template")
    public void downloadTemplate(HttpServletResponse response) {
        moldBiz.downloadTemplate(response);
    }

    /**
     * 分页查询模具表
     *
     * @param basePageReq
     * @return Result
     */
    @PreAuth(value = "erp:mold:select", description = "查询模具")
    @PostMapping("/api/erp/mold/selectMoldPage")
    public Result<Paging<MoldResp>> selectMoldPage(@RequestBody @Validated BasePageReq<MoldQueryReq> basePageReq) {
        return Result.data(moldBiz.selectMoldPage(basePageReq.toPage(), basePageReq.getQuery()));
    }
}
